package io.mft.as.ar.gateway.client;

import cn.hutool.core.util.ObjectUtil;
import io.mft.as.ar.gateway.constants.GatewayConstants;

/**
 * 描述: 网关服务地址类
 * 版权: Copyright (c) 2013 
 * 公司: 思迪科技 
 * 作者: 李建 
 * 版本: 1.0 
 * 创建日期: 2013-1-21 
 * 创建时间: 下午4:35:41
 */
public class NetAddress
{

	private String IP = "";

	private int port = 0;

	private boolean isAlive = false;

	private String serverType = "";   //服务器类型 socket/http/https

	private String scanAddr = "";   //http服务器扫描地址

	private String requestAddr = "";   //http服务器接口请求地址

	public NetAddress()
	{

	}

	public NetAddress(String serverType, String IP, int port, String httpScanAddr, String httpRequestAddr)
	{

		this.serverType = serverType;
		this.IP = IP;
		this.port = port;

		StringBuilder serverAddrBuffer = new StringBuilder();
		if(GatewayConstants.SCAN_SERVERTYPE_HTTPS.equalsIgnoreCase(serverType))
		{
			serverAddrBuffer.append(GatewayConstants.SCAN_SERVERTYPE_HTTPS).append("://");
		}
		else
		{
			serverAddrBuffer.append(GatewayConstants.SCAN_SERVERTYPE_HTTP).append("://");
		}
		serverAddrBuffer.append(IP);
		if(port != 80)
		{
			serverAddrBuffer.append(":").append(port);
		}

		if(httpScanAddr.startsWith("/"))
		{
			scanAddr = serverAddrBuffer.toString() + httpScanAddr;
		}
		else
		{
			scanAddr = serverAddrBuffer.toString() + "/" + httpScanAddr;
		}

		if(httpRequestAddr.startsWith("/"))
		{
			requestAddr = serverAddrBuffer.toString() + httpRequestAddr;
		}
		else
		{
			requestAddr = serverAddrBuffer.toString() + "/" + httpRequestAddr;
		}

	}

	public String getIP()
	{
		return IP;
	}

	public void setIP(String IP)
	{
		this.IP = IP;
	}

	public int getPort()
	{
		return port;
	}

	public void setPort(int port)
	{
		this.port = port;
	}

	public boolean isAlive()
	{
		return isAlive;
	}

	public void setAlive(boolean alive)
	{
		isAlive = alive;
	}

	public String getServerType()
	{
		if(ObjectUtil.isEmpty(serverType))
		{
			//默认是socket类型
			return GatewayConstants.SCAN_SERVERTYPE_SOCKET;
		}
		else
		{
			return serverType;
		}
	}

	public String getScanAddr()
	{
		return scanAddr;
	}

	public String getRequestAddr()
	{
		return requestAddr;
	}

}
